<?php

namespace app\admin\model;

class BannerModel extends BaseModel
{
    /**
     * 主键
     * @var string
     */
    protected $pk = 'admin_id';
    /**
     * 表名
     * @var string
     */
    protected $table = "web_banner";

    public function list($param = [], $field = "*")
    {
        $where = [];
        if (isset($param['key']) && !empty($param['key'])) {
            $where[] = ['title', 'like', "%{$param['key']}%"];
        }
        $where[] = ['is_delete', 'eq', 1];
        return $this->where($where)->field($field)->order('id desc')->paginate(array(
            'list_rows' => $param['pageSize'],
            'page'      => $param['page']
        ))->toArray();
    }

    /**
     * 增加
     */
    public function mInsert($param = [])
    {
        return $this->insertGetId($param);
    }

    /**
     * 修改
     * @param array $param
     * @param $id
     * @return MenuModel
     */
    public function mUpdate($param = [], $id)
    {
        return $this->where([
            'id' => $id
        ])->update($param);
    }

    /**
     * 根据id获取信息
     * @param $id
     * @param string[] $field
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getInfoById($id, $field= "*")
    {
        return $this->where([
            'id' => $id
        ])->field($field)->find();
    }

    /**
     * 获取有效数据
     * @param array $where 查询条件
     * @param string $field 查询字段，默认所有
     * @return array|\PDOStatement|string|\think\Collection|\think\model\Collection
     */
    public function getDatasByWhere($where = [], $field = "*")
    {
        return $this->where($where)->field($field)->select();
    }
}